import wtforms
from orms import UserORM
from wtforms.validators import Email, Length, EqualTo
import email_validator


class RegisterForm(wtforms.Form):
    email = wtforms.StringField(validators=[Email(message="邮箱格式错误!")])
    captcha = wtforms.StringField(validators=[Length(min=4, max=4, message="验证码格式错误！")])
    nick_name = wtforms.StringField(validators=[Length(min=6, max=20, message="用户名格式错误!")])
    password = wtforms.StringField(validators=[Length(min=6, max=20, message="密码格式错误!")])
    password_confirm = wtforms.StringField(validators=[EqualTo("password")])

    # 邮箱是否已经被使用
    # 验证码是否正确
    def validate_email(self, filed):
        email = filed.data
        user = UserORM.query.filter_by(email=email).first()
        if user:
            raise wtforms.ValidationError(message="该邮箱已经被注册！")
